/**
 * Event system types for ComfyUI SDK
 */

// Base event interface
export interface BaseEvent {
  type: string;
  timestamp: Date;
  promptId?: string;
}

// Progress events
export interface ProgressEvent extends BaseEvent {
  type: 'progress';
  nodeId: string;
  progress: number;
  max: number;
}

// Execution events
export interface ExecutingEvent extends BaseEvent {
  type: 'executing';
  nodeId: string;
}

export interface ExecutedEvent extends BaseEvent {
  type: 'executed';
  nodeId: string;
  outputs: Record<string, unknown>;
  executionTime: number;
}

// Error events
export interface ErrorEvent extends BaseEvent {
  type: 'error';
  nodeId?: string;
  error: Error;
  details?: unknown;
}

// Connection events
export interface ConnectedEvent extends BaseEvent {
  type: 'connected';
  clientId?: string;
}

export interface DisconnectedEvent extends BaseEvent {
  type: 'disconnected';
  reason?: string;
}

// Queue events
export interface QueuedEvent extends BaseEvent {
  type: 'queued';
  position: number;
}

export interface StartedEvent extends BaseEvent {
  type: 'started';
}

export interface CompletedEvent extends BaseEvent {
  type: 'completed';
  outputs: Record<string, unknown>;
  executionTime: number;
}

// Status events
export interface StatusEvent extends BaseEvent {
  type: 'status';
  data: unknown;
}

export interface CachedEvent extends BaseEvent {
  type: 'cached';
  nodes: string[];
}

// Union type for all events
export type ComfyUIEvent =
  | ProgressEvent
  | ExecutingEvent
  | ExecutedEvent
  | ErrorEvent
  | ConnectedEvent
  | DisconnectedEvent
  | QueuedEvent
  | StartedEvent
  | CompletedEvent
  | StatusEvent
  | CachedEvent;

// Event listener types
export type EventListener<T extends ComfyUIEvent = ComfyUIEvent> = (event: T) => void;

export interface EventListeners {
  progress?: EventListener<ProgressEvent>;
  executing?: EventListener<ExecutingEvent>;
  executed?: EventListener<ExecutedEvent>;
  error?: EventListener<ErrorEvent>;
  connected?: EventListener<ConnectedEvent>;
  disconnected?: EventListener<DisconnectedEvent>;
  queued?: EventListener<QueuedEvent>;
  started?: EventListener<StartedEvent>;
  completed?: EventListener<CompletedEvent>;
  status?: EventListener<StatusEvent>;
  cached?: EventListener<CachedEvent>;
}

// Event emitter interface
export interface EventEmitter {
  on<T extends ComfyUIEvent>(eventType: T['type'], listener: EventListener<T>): void;
  off<T extends ComfyUIEvent>(eventType: T['type'], listener: EventListener<T>): void;
  emit<T extends ComfyUIEvent>(event: T): void;
  removeAllListeners(eventType?: string): void;
}
